package com.pinc.akb48;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler_Version extends DefaultHandler {
	public SoftVer sv;
	private boolean b_appname;
	private boolean b_vers;
	private boolean b_feat;
	private boolean finish;
	private String appname;

	public XMLHandler_Version(String name) {
		super();
		finish = false;
		b_appname = b_vers = b_feat = false;
		appname = name;
		sv = new SoftVer();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (localName.length() > 0 && localName.compareToIgnoreCase("app") == 0) {

		}

		if (!finish) {
			if (localName.length() > 0
					&& localName.compareToIgnoreCase("name") == 0) {
				b_appname = true;
			} else if (localName.length() > 0
					&& localName.compareToIgnoreCase("version") == 0) {
				b_vers = true;
			} else if (localName.length() > 0
					&& localName.compareToIgnoreCase("feature") == 0) {
				b_feat = true;
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
	throws SAXException {

		if (localName.length() > 0 && localName.compareToIgnoreCase("app") == 0) {
			if (sv.name.compareToIgnoreCase(appname) == 0) {
				finish = true;
			}
		}

		if (localName.length() > 0
				&& localName.compareToIgnoreCase("name") == 0) {
			b_appname = false;
		} else if (localName.length() > 0
				&& localName.compareToIgnoreCase("version") == 0) {
			b_vers = false;
		} else if (localName.length() > 0
				&& localName.compareToIgnoreCase("feature") == 0) {
			b_feat = false;
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {
		if (b_appname) {
			sv.name = new String(ch, start, length);
		} else if (b_vers)
			sv.version = Integer.parseInt(new String(ch, start, length));
		else if (b_feat)
			sv.feature = new String(ch, start, length);
	}

	public class SoftVer {
		public String name;
		public int version;
		public String feature;
		public SoftVer() {
			super();
			this.version = 0;
		}
	}
}
